﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.Model;

namespace OgilvyOne.SqlServerDAL
{
    public class RolePopedom: IRolePopedom
    {
        public Hashtable getUserPopedomMenu(string userrole)
        {
            string sql = "select columntag, operation from cms_operate t where t.rid in (" + userrole + ") group by columntag, operation";
            Hashtable map = new Hashtable();
            SqlParameter[] parms = new SqlParameter[] { };
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, sql, parms))
            {
                while (rdr.Read())
                {
                    string key = rdr.GetString(0);
                    if (key != "")
                    {
                        string pope = rdr.GetString(1);
                        if (map.ContainsKey(key))
                        {
                            pope += map[key].ToString();

                        }
                        map.Add(key, pope);
                    }
                }
            }
            return map;
        }

        private const string DEL_POPEDOMOPINTON_SQL = "delete from cms_operate where rid = @rid";
        private const string INSERT_POPEDOMOPINTON_SQL = "insert into cms_operate(rid,COLUMNTAG,OPERATION,ADDTIME) values (@rid,@COLUMNTAG,@OPERATION,getDate())";
        public void saveRolePopedom(int rid, IList popedomlist)
        {
            SqlParameter[] itemParms = new SqlParameter[]{
                new SqlParameter("@rid", SqlDbType.Int, 4,"rid")
            };
            itemParms[0].Value = rid;
            
            using (SqlConnection conn = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(trans, CommandType.Text, DEL_POPEDOMOPINTON_SQL, itemParms);

                        for (int i = 0; i < popedomlist.Count; i++)
                        {
                            Hashtable map = (Hashtable)popedomlist[i];
                            SqlParameter[] parm = new SqlParameter[]{
                                new SqlParameter("@rid", SqlDbType.Int, 4,"rid"),
                                new SqlParameter("@COLUMNTAG", SqlDbType.VarChar, 200,"COLUMNTAG"),
                                new SqlParameter("@OPERATION", SqlDbType.VarChar, 255,"OPERATION")
                            };
                            parm[0].Value = rid;
                            parm[1].Value = (string)map["layerid"];
                            parm[2].Value = (string)map["popedom"];
                            SQLHelper.ExecuteNonQuery(trans, CommandType.Text, INSERT_POPEDOMOPINTON_SQL, parm);

                        }

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        throw;
                    }
                }
            }
        }

        private const string COLUMN_LIST_SQL = "select c.classid,c.classname,c.layerid,ct.backlink,parentid,isauditing,COLUMNTYPE from cms_column c left join cms_column_type ct on c.columntype=ct.columntypeid  where c.classid<> 1 order by layerid, c.orderid";
        public IList getColumnList()
        {
            IList datalist = new ArrayList();
            SqlParameter[] parms = new SqlParameter[] { };
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, COLUMN_LIST_SQL, parms))
            {
                while (rdr.Read())
                {
                    UserColumnModel uc = new UserColumnModel();
                    uc.Columnid = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        uc.Columnname = rdr.GetString(1);
                    if (!rdr.IsDBNull(2))
                        uc.Layerid = rdr.GetString(2);
                    if (!rdr.IsDBNull(3))
                        uc.Columnurl = rdr.GetString(3);
                    if (!rdr.IsDBNull(4))
                        uc.Parentid = rdr.GetInt32(4);
                    if (!rdr.IsDBNull(5))
                        uc.Audit = rdr.GetInt32(5).ToString();
                    uc.Columntypeid = rdr.GetInt32(6);
                    datalist.Add(uc);
                }
            }

            return datalist;
        }

        private const string SYS_COLUMN_LIST_SQL = "select id,classname,layerid,linkurl,parentid from cms_syscolumn order by layerid, orderid";
        public IList getSysColumnList()
        {
            IList datalist = new ArrayList();
            SqlParameter[] parms = new SqlParameter[] { };
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SYS_COLUMN_LIST_SQL, parms))
            {
                while (rdr.Read())
                {
                    UserColumnModel uc = new UserColumnModel();
                    uc.Columnid = rdr.GetInt32(0);
                    uc.Columnname = rdr.GetString(1);
                    uc.Layerid = rdr.GetString(2);
                    uc.Columnurl = rdr.GetString(3);
                    uc.Parentid = rdr.GetInt32(4);
                    datalist.Add(uc);
                }
            }

            return datalist;
        }
    }
}
